from problem import *
from osm_utils4 import *
from all import *

class Move(ProblemAction):
	def __init__(self, start, target, cost):
		self.start = start;
		self.target = target;
		self.cost = cost;
		
	def __cmp__(self, other):
		if self.start != other.start:
			return self.start.__cmp__(other.start);
		return self.target.__cmp__(other.target);
	
	def __hash__(self):
		return hash(self.start + self.target);
	
	def __str__(self):
		return "Move from " + str(self.start) + " to " + str(self.target) + " with cost " + str(self.cost);

class Location(ProblemState):
	
	def __init__(self, identity, goal, costCalculator, countryMap, parent):
		self.identity = identity;
		self.goal = goal;
		self.costCalculator = costCalculator;
		self.countryMap = countryMap;
		self.parent = parent;
		
	def getSuccessors(self):
		successors = dict();
		myJunction = self.countryMap.GetJunction(self.identity);
		for l in myJunction.links:
			target = l.target;
			if target == self.parent:
				continue;
			cost = float(l.distance);
			successors[Move(self.identity, target, cost)] = Location(target, self.goal, self.costCalculator, self.countryMap, self.identity);
		return successors;
	
	def isGoal(self):
		return (self.identity == self.goal);
	
	def __cmp__(self, other):
		return self.identity.__cmp__(other.identity);
	
	def __hash__(self):
		return hash(self.identity);
	
	def __str__(self):
		return "Location: " + str(self.identity);


def CreateCostCalculator(weightDistance, weightTime, weightPetrol):
	calculator = lambda l: l.distance;
	return calculator;


class myHeuristic:
	def __init__(self, countryMap):
		self.countryMap = countryMap;
	
	def evaluate(self, state):
		j1 = self.countryMap.GetJunction(state.identity);
		j2 = self.countryMap.GetJunction(state.goal);
		return self.countryMap.ComputeDistance(j1.lat, j1.lon, j2.lat, j2.lon); 



if __name__ == "__main__":	
	searchEngine = AStar();
	m = CountryMap();
	m.LoadMap2();
	h = myHeuristic(m);
	c = CreateCostCalculator(1,0,0);
	print searchEngine.find(Location(12345, 552054, c,m, -1),h)
	print searchEngine.find(Location(12345, 12347, c,m, -1),h) 
	print searchEngine.find(Location(12345, 5227, c,m, -1),h) 
	print searchEngine.find(Location(12345, 12348, c,m, -1),h) 
	

